import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable';


/**
 * 工具条服务
 *
 * @export
 * @class ToolbarService
 */
@Injectable()
export class ToolbarService {

  private _commands: Command[] = [];
  /**
   * 获得命令
   *
   * @readonly
   *
   * @memberof ToolbarService
   */
  get commands() {
    return this._commands;
  }


  constructor() {
  }

  /**
   * 注册工具条上的命令
   *
   * @param {Command} command 命令对象
   *
   * @memberOf ToolbarService
   */
  register(command: Command) {
    //在command.index位置移除0个元素，后添加command
    this.commands.splice(this.commands.length, 0, command);
  }

}


/**
 * 命令
 *
 * @export
 * @class Command
 */
export class Command {


  /**
   * 创建一个命令对象实例。
   * @param {string} name 命令名称
   * @param {string} title 命令标题
   * @param {string} icon 命令图标
   * @param {string} permission 权限许可标识   *
   * @param {(event) => void} executeMethod 命令需要执行的方法
   * @param {() => void} canExecuteMethod 判断命令是否可以执行
   * @param {number?} index 序号
   * @memberof Command
   */
  constructor(
    public name: string,
    public title: string,
    public icon: string,
    public permission: string,
    public executeMethod: (enity: any, event?: any) => void,
    public canExecuteMethod: (event: any) => boolean | Observable<boolean> | Promise<boolean>,
    public index?: number,
  ) {
  }
}


// WEBPACK FOOTER //
// ./src/app/shared/toolbar.service.ts
